import {
  request
} from "~/utils/request.js"

// pages/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    show: false,
    id: "",
    config: {}
  },
  getConfig() {
    request("member/config").then(res => {
      console.log(res)
      this.setData({
        config: res
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.id) {
      this.getDetail(options.id)
    } else {
      this.setData({
        detail: wx.getStorageSync('enterDetail'),
      })
      this.onCanShow()
    }
    this.getConfig()

  },
  onCanShow() {
    this.setData({
      id: this.data.detail.uid
    })
    this.data.detail.state == 0 && this.onChange()
  },
  getDetail(id) {
    wx.login({
      success: (res) => {
        return request("member/login", {
          code: res.code
        }, "post").then(ret => {
          console.log(ret)
          wx.setStorageSync('token', ret.token)
          request("member/visit/" + id).then(res => {
            this.setData({
              detail: res,
            })
            this.onCanShow()
          })
        })
      },
    })

  },
  onSureLeave() {
    request(`member/visit/${this.data.id}/leave`, {}, "post").then(res => {
      wx.showToast({
        title: "已保存",
        icon: 'none'
      })
      this.onChange()
    })
  },
  onChange() {
    this.setData({
      show: !this.data.show
    })
  }
})